/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TextPropertyView.java
 *
 * Created on Jan 2, 2012, 10:31:26 PM
 */
package propertychangestest;

import com.andromeda.saturno.property.Property;
import com.andromeda.saturno.property.change.SetValuePropertyChange;
import com.andromeda.saturno.property.event.ValueChangeEvent;
import com.andromeda.saturno.property.event.ValueChangeListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

/**
 *
 * @author Dickinson Román Arismendy Torres
 */
public class TextPropertyView extends javax.swing.JPanel implements ValueChangeListener<String> {

    private Property<String> property;

    /** Creates new form TextPropertyView */
    public TextPropertyView() {
        initComponents();
        txtValue.getDocument().addDocumentListener(new DocumentListener() {

            @Override
            public void insertUpdate(DocumentEvent e) {
                if (!externalChange) {
                    internalChange = true;
                    property.makeChange(new SetValuePropertyChange<String>(txtValue.getText()));
                    internalChange = false;
                }
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                if (!externalChange) {
                    internalChange = true;
                    property.makeChange(new SetValuePropertyChange<String>(txtValue.getText()));
                    internalChange = false;
                }
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
            }
        });
    }

    public Property<String> getProperty() {
        return property;
    }

    public void setProperty(Property<String> property) {
        this.property = property;
        bindToUI();
        if (property != null) {
            property.addValueChangeListener(this);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txtValue = new javax.swing.JTextField();
        btnUndoAll = new javax.swing.JButton();
        btnUndo = new javax.swing.JButton();
        btnRedo = new javax.swing.JButton();
        btnRedoAll = new javax.swing.JButton();

        btnUndoAll.setText("Undo all");
        btnUndoAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUndoAllActionPerformed(evt);
            }
        });

        btnUndo.setText("Undo");
        btnUndo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUndoActionPerformed(evt);
            }
        });

        btnRedo.setText("Redo");
        btnRedo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRedoActionPerformed(evt);
            }
        });

        btnRedoAll.setText("Redo All");
        btnRedoAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRedoAllActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(txtValue, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 274, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(btnUndoAll)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnUndo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnRedo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnRedoAll)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(txtValue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnUndoAll)
                    .addComponent(btnUndo)
                    .addComponent(btnRedo)
                    .addComponent(btnRedoAll))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnUndoAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUndoAllActionPerformed
        if (property != null) {
            property.undoAll();
        }
    }//GEN-LAST:event_btnUndoAllActionPerformed

    private void btnUndoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUndoActionPerformed
        if (property != null) {
            property.undo();
        }
    }//GEN-LAST:event_btnUndoActionPerformed

    private void btnRedoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRedoActionPerformed
        if (property != null) {
            property.redo();
        }
    }//GEN-LAST:event_btnRedoActionPerformed

    private void btnRedoAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRedoAllActionPerformed
        if (property != null) {
            property.redoAll();
        }
    }//GEN-LAST:event_btnRedoAllActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnRedo;
    private javax.swing.JButton btnRedoAll;
    private javax.swing.JButton btnUndo;
    private javax.swing.JButton btnUndoAll;
    private javax.swing.JTextField txtValue;
    // End of variables declaration//GEN-END:variables
    boolean externalChange = false;
    boolean internalChange = false;

    @Override
    public void valueChanged(ValueChangeEvent<String> evt) {
        bindToUI();
    }

    private void bindToUI() {
        if (!internalChange) {
            externalChange = true;
            txtValue.setText(property == null ? null : property.getValue());
            externalChange = false;
        }
    }
}
